<?php 

/**
 * 发送短信
 * @param $tel 手机号码
 * @param $content 发送内容
 * @param string $type
 * @return string
 */
function SendMsg($tel,$content,$type=''){
	$num = 2;
	if($type){
		$phone=implode(',',$tel);
	}else{
		$phone=$tel;
	}

	$account = 'yddujia';
	$pwd = '8BBA5D3D93C777F2A526233913A6'; // blank0725的md5编码
	$date = date("Y-m-d H:i:s");

	//$content = iconv("utf-8","gbk",$content."[签名]");
	//$content = urlencode($content);
	$url = "http://web.cr6868.com/asmx/smsservice.aspx?name=".$account."&pwd=".$pwd."&content=".$content."&mobile=".$phone."&stime=".$date."&sign=义达生态旅游&type=pt&extno=";

	//$url = 'http://web.cr6868.com/asmx/smsservice.aspx';
	$data['name'] = $account;
	$data['pwd'] = $pwd;
	$data['content'] = $content;
	$data['mobile'] = $phone;
	$data['stime'] = $date;
	$data['sign'] = '义达生态旅游';
	$data['type'] = 'pt';
	$data['extno'] = '';

	$info = postSMS($url, $data);
	return $info;
}

/**
 * POST提交短信数据
 */
function postSMS($url,$data=''){
	$row = parse_url($url);
	$host = $row['host'];
	$port = $row['port'] ? $row['port']:80;
	$file = $row['path'];
	while (list($k,$v) = each($data)){
		$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
	}
	$post = substr( $post , 0 , -1 );
	$len = strlen($post);
	$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
	if (!$fp) {
		return "$errstr ($errno)\n";
	} else {
		$receive = '';
		$out = "POST $file HTTP/1.1\r\n";
		$out .= "Host: $host\r\n";
		$out .= "Content-type: application/x-www-form-urlencoded\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Content-Length: $len\r\n\r\n";
		$out .= $post;
		fwrite($fp, $out);
		while (!feof($fp)) {
			$receive .= fgets($fp, 128);
		}
		fclose($fp);
		$receive = explode("\r\n\r\n",$receive);
		unset($receive[0]);
		return implode("",$receive);
	}
}

/**
 * 设置分页
 * @param $model 检索的model
 * @param $perPageCount 每页显示的记录数
 * @param $where 检索条件
 */
function setPage($model, $where, $perPageCount){
    if(isset($model)){
        if(isset($where)){
            $count = $model->where($where)->count();// 查询满足要求的总记录数
            //分页跳转的时候保证查询条件
            foreach($where as $key=>$val) {
                $Page->parameter[$key]   =   urlencode($val);
                $_SESSION[$key] = urlencode($val);
            }
        } else {
            $count = $model->count();// 查询满足要求的总记录数
        }
        if(intval($perPageCount ) == 0){
            $perPageCount = 15;
        }
        $page = new  \Think\Page($count, $perPageCount);// 实例化分页类 传入总记录数和每页显示的记录数
        // 设置分页样式
        $page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '尾页');
        $page->setConfig('first', '首页');
        $page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $page->lastSuffix = false;//最后一页不显示为总页数
        return $page;
    } else {
        return null;
    }
}

/**
 * 设置分页(我的订单页面用)
 * @param $model 检索的model
 * @param $perPageCount 每页显示的记录数
 * @param $where 检索条件
 */
function setPageForOrder($model, $where, $perPageCount){
    if(isset($model)){
        if(isset($where)){
            $count = $model->where($where)->count();// 查询满足要求的总记录数
            //分页跳转的时候保证查询条件
            foreach($where as $key=>$val) {
                $Page->parameter[$key]   =   urlencode($val);
                $_SESSION[$key] = urlencode($val);
            }
        } else {
            $count = $model->count();// 查询满足要求的总记录数
        }
        if(intval($perPageCount ) == 0){
            $perPageCount = 15;
        }
        $page = new  \Think\Page($count, $perPageCount);// 实例化分页类 传入总记录数和每页显示的记录数
        // 设置分页样式
        $page->setConfig('prev', '上页');
        $page->setConfig('next', '下页');
        $page->lastSuffix = false;//最后一页不显示为总页数
        return $page;
    } else {
        return null;
    }
}

/**
 * 邮件发送函数
 */
function sendMail($to, $title, $content) {
     
    Vendor('PHPMailer.PHPMailerAutoload');
    $mail = new PHPMailer(); //实例化
    $mail->isSMTP(); // 启用SMTP
    $mail->Host=C('MAIL_HOST'); //smtp服务器的名称（这里以QQ邮箱为例）
    $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
    $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
    $mail->Password = C('MAIL_PASSWORD') ; //邮箱密码
    $mail->From = C('MAIL_FROM'); //发件人地址（也就是你的邮箱地址）
    $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
    $mail->AddAddress($to,"尊敬的客户");
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
    $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
    $mail->Subject =$title; //邮件主题
    $mail->Body = $content; //邮件内容
    $mail->AltBody = "这是一个纯文本的非HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
    return  ($mail->Send());
}

/**
 * 设置当前访问页面的信息
 * @param string $currentSide 当前访问的页面所属的导航栏
 */
function setCurrentPageInfo($currentSide="", $currentPageUrl){
    $_SESSION['current_side'] = $currentSide;
    if(empty($currentPageUrl)){
        $_SESSION['current_page_url'] = $_SERVER['REQUEST_URI'];
    } else {
        $_SESSION['current_page_url'] = $currentPageUrl;
    }
}
?>